/* Copyright (c) 2004-2022 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.core.domain;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.annotation.Comment;
import info.peigen.hotpot.common.core.enums.Gender;
import info.peigen.hotpot.common.core.enums.YesOrNo;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

/**
 * <b>(Identity)</b>
 * 身份证件
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/27
 */
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class Identity {
    @Comment(value = "名")
    String   firstName;
    @Comment(value = "姓")
    String   lastName;
    @Comment(value = "名-拼音")
    String   firstNameSpell;
    @Comment(value = "姓-拼音")
    String   lastNameSpell;
    @Comment(value = "身份证号")
    String   number;
    @Comment(value = "性别")
    Gender   gender;
    @Comment(value = "籍贯")
    String   nativePlace;
    @Comment(value = "出生日期")
    String   birthday;
    @Comment(value = "地址")
    Address  address;
    @Comment(value = "身份证照片-正面")
    Image    frontImage;
    @Comment(value = "身份证照片-背面")
    Image    backImage;
    @Comment(value = "手持身份证照片")
    Image    holdingIdImage;
    @Comment(value = "有效期起始")
    DateTime periodOfValidityStart;
    @Comment(value = "有效期截至")
    DateTime periodOfValidityEnd;
    @Comment(value = "是否长期")
    @Builder.Default
    YesOrNo  periodOfValidityLongTerm = YesOrNo.N;

    public void check() {
        Assert.isTrue(StrUtil.isAllNotBlank(firstName, firstNameSpell, lastName, lastNameSpell, number));
        Assert.isTrue(ObjectUtil.isAllNotEmpty(address, frontImage, backImage, holdingIdImage, periodOfValidityStart, periodOfValidityEnd, periodOfValidityLongTerm));
        address.notEmptyCheck();
        frontImage.check();
        backImage.check();
        holdingIdImage.check();
    }

    /**
     * 验证是否为身份证号码（支持18位、15位和港澳台的10位）
     *
     * @param value 身份证号，支持18位、15位和港澳台的10位
     *
     * @return 是否为有效身份证号码
     */
    public boolean isCitizenId(CharSequence value) {
        return Validator.isCitizenId(String.valueOf(value));
    }

    /**
     * 验证是否为生日<br>
     * 只支持以下几种格式：
     * <ul>
     * <li>yyyyMMdd</li>
     * <li>yyyy-MM-dd</li>
     * <li>yyyy/MM/dd</li>
     * <li>yyyy.MM.dd</li>
     * <li>yyyy年MM月dd日</li>
     * </ul>
     *
     * @param value 值
     *
     * @return 是否为生日
     */
    public boolean isBirthday(CharSequence value) {
        return Validator.isBirthday(value);
    }
}